package com.lovo.project.servlet.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.project.enums.ServiceCodeEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.ServiceDO;
import com.lovo.project.service.ServiceService;
import com.lovo.project.service.impl.ServiceServiceImpl;
import com.lovo.project.vo.ResultVO;
import com.sun.xml.internal.bind.v2.TODO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ListByShopIdServlet",value = "/service/listByShopId")
public class ListByShopIdServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            ServiceService serviceService =new ServiceServiceImpl();
            Integer shopId = Integer.parseInt(request.getParameter("shopId"));
            String page = request.getParameter("page");
            String size = request.getParameter("size");
            PageHelper.startPage(page == null ? 1 : Integer.parseInt(page),
                    size == null ? 6 : Integer.parseInt(size));
            
            
            List<ServiceDO> list = serviceService.listByShopId(shopId);
            PageInfo<ServiceDO> pageInfo = PageInfo.of(list);
            
            response.getWriter().print(ResultVO.success().add(pageInfo).toJson());
//            response.getWriter().print(ResultVO.success().add(list).toJson());
        }catch (OurException e){
            response.getWriter().println(ResultVO.fail(ServiceCodeEnum.SERVICE_FIND_ERROR).toJson());
        }catch (Exception e1){
            response.getWriter().println(ResultVO.fail(ServiceCodeEnum.SERVICE_UNKNOWN_ERROR).toJson());
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
